package ooday04;

import javax.swing.*;

//多态的演示
public class PolymorphicDemo {
    public static void main(String[] args) {

        //演示向上造型
        /*
        Dog o1 = new Dog("小黑",2,"黑");//狗是狗
        //o1.name/age/color/eat()/drink()/swim()/looHome();只能访问这些
        System.out.println(o1.name);
        System.out.println(o1.age);
        System.out.println(o1.color);
        o1.eat();
        o1.drink();
        o1.swim();
        o1.LookHome();
        System.out.println();


        Animal o2 = new Dog("小黑",2,"黑");//狗是动物--向上类型
        //o2.eat()/name/age/color/drink()
        System.out.println(o2.age);
        System.out.println(o2.name);
        System.out.println(o2.color);
        o2.eat();
        o2.drink();
        System.out.println();


        Swim o3 = new Dog("小黑",2,"黑");//狗会游泳--向上类型
       // o3.swim();

        */

        //演示向上类型（多态）的第1点应用：
       /*
        Master master = new Master();

        Dog dog =new Dog("中华田园",2,"黄色");
//      Animal dog = new Dog(.........................);

        Chick chick = new Chick("小花",3,"花");
        Fish fish = new Fish("小金",1,"金");
        master.feed(dog);//传参的同时，系统自动做了造型
        master.feed(chick);
        master.feed(fish);
        */

        //演示向上类型（多态）的第2点应用：
       /*
        //Animal o = new Animal();编译错误,抽象类不能被new
        Animal[] animals = new Animal[5];
        animals[0] =new Dog("中华田园",2,"黄色");
        animals[1] =new Dog("金毛",4,"金色");
        animals[2] =new Fish("小白",1,"白");
        animals[3] = new Fish("小黄",1,"黄");
        animals[4] = new Chick("小灰",3,"灰");

        for (int i=0;i< animals.length;i++){//遍历所有的动物
            System.out.println(animals[i].name);//输出每个动物的名字
            animals[i].drink();//每个动物喝水
            animals[i].eat();//每个动物吃饭
        }

        */

        //演示向下造型/强制类型转换，成功的条件：（大转小）
/*         引用所指向的对象，就是该类型；
            ............. 实现了该接口或继承了该类
 */
        /*
        Animal o1 =new Dog("黑",2,"小黑");
        //o1所指的对象（new后面的）是Dog 实现了Swim接口
        并继承了Animal 所以可以转换为Dog   Swim Animal

        Animal o = new Dog("小黑",2,"黑");//向上造型
        Dog g =(Dog)o;//引用o所指向的对象时Dog，就是Dog类型
        Swim s = (Swim)o;//引用o所指向的对象是Swim，实现了Swim接口
       // Fish f = (Fish) o;//因为dog没有实现该接口以及继承该类
       运行时会发生ClassCastException类型转换异常

        System.out.println(o instanceof Dog);//true
        System.out.println(o instanceof Swim);//true
        System.out.println(o instanceof Fish);//false
        if (o instanceof Fish){ //false 不再执行  转换时先用instanceof 判断
            Fish f = (Fish) o;
        }false

         */


        Animal[] animals = new Animal[5];
        animals[0] = new Dog("中华田园",2,"黄色");
        animals[1] = new Dog("金毛",4,"金色");
        animals[2] = new Fish("小白",1,"白");
        animals[3] = new Fish("小黄",1,"黄");
        animals[4] = new Chick("小灰",3,"灰");

        for (int i=0;i< animals.length;i++){//遍历所有的动物
            System.out.println(animals[i].name);//输出每个动物的名字
            animals[i].drink();//每个动物喝水
            animals[i].eat();//每个动物吃饭

            if (animals[i] instanceof Dog){//只能适用于Dog类
                Dog dog =(Dog) animals[i];
                dog.LookHome();
                dog.swim();
            }

            if (animals[i] instanceof Fish){//an 转换F访问f
                Fish fish = (Fish)animals[i];
                fish.swim();

            }
            if (animals[i] instanceof Chick){//只能适用于Chick类
                Chick chick = (Chick) animals[i];
                chick.layEggs();
            }

            if (animals[i] instanceof Swim){//适用于所有实现Swim接口的（会游泳的）
                Swim s = (Swim) animals[i];
                s.swim();//充当狗和鱼游泳
            }










        }









    }
}
